
function getTime(value) {
    if(value == undefined){
        return;
    }
    let date = new Date(value * 1000);
    // let date = new Date(value);
    //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM; //月补0
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d; //天补0
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h; //小时补0
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m; //分钟补0
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s; //秒补0
    // return y + '-' + MM + '-' + d; //年月日
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; //年月日时分秒
}

/**设置倒计时**/
function setmiaotimes(endtime) {
    setInterval(function () {
        var str = "";
        var nowdate = new Date();  //获取当前时间
        //  console.log(nowdate);    //返回当前时间
        var nowtime = nowdate.getTime() /1000;
        // console.log(nowtime);  //返回毫秒数
        // var str = "2022/12/21 08:30";  //设置结束时间
        // var enddate = new Date(str);
        // console.log(enddate);
        // var endtime = enddate.getTime();
        var lefttime = endtime - nowtime;   //计算剩余时间
        // console.log(lefttime)
        // var days = Math.floor(lefttime / 1000 / 60 / 60 / 24);  //计算天数   1s=1000毫秒
        var hours = Math.floor(lefttime  / 60 / 60 % 24);
        var mins = Math.floor(lefttime  / 60 % 60);
        var miao = Math.floor(lefttime  % 60);
        // console.log(nowdate)
        // console.log(days)
        // console.log(hours)
        // console.log(mins)
        // console.log(miao)
        //在body中输出字符
        // `剩余${hh}时${mm}分${ss}秒`
         str =   `${hours}:${mins}:${miao}`;
        console.log(`${str}`)
        return `${str}`;
        // document.body.innerHTML = ('<h1>' + "距离2022年12月21日8:30还剩：" + days + "天" + hours + "时" + mins + "分" + miao + "秒" + '</h1>')
    }, 1000)
}

//倒计时10分钟
function limitTimeFilter (endtime) {
    var showTime = '';
    var nowdate = new Date();  //获取当前时间
    var nowtime = nowdate.getTime() /1000;
    var rightTime = endtime - nowtime;   //计算剩余时间
    if (rightTime > 0) {
        //判断剩余倒计时时间如果大于0就执行倒计时否则就结束
        var dd = Math.floor(rightTime / 60 / 60 / 24) < 10 ? '0' + Math.floor(rightTime / 60 / 60 / 24) : Math.floor(rightTime / 60 / 60 / 24);
        var hh = Math.floor((rightTime / 60 / 60) % 24) < 10 ? '0' + Math.floor((rightTime / 60 / 60) % 24) : Math.floor((rightTime / 60 / 60) % 24);
        var mm = Math.floor((rightTime / 60) % 60) < 10 ? '0' + Math.floor((rightTime / 60) % 60) : Math.floor((rightTime / 60) % 60);
        var ss = Math.floor((rightTime) % 60) < 10 ? '0' + Math.floor((rightTime) % 60) : Math.floor((rightTime) % 60);
        if(dd>0){
            showTime = `剩余${dd}天${hh}时${mm}分${ss}秒`
        }else{
            if(hh>0){
                showTime = `剩余${hh}时${mm}分${ss}秒`
            }else{
                if(mm>0){
                    showTime = `剩余${mm}分${ss}秒`
                }else{
                    if(ss>0){
                        showTime = `剩余${ss}秒`
                    }
                }
            }
        }
    } else {
        var showTime = "";
    }
    return showTime;
}




function numFilter (value) {
    // 截取当前数据到小数点后两位
    let realVal = parseFloat(value).toFixed(2)
    return realVal
}

// 一定要暴露出去

export default {
    getTime,
    numFilter,
    setmiaotimes,
    limitTimeFilter
}